

//  此函数执行会返回一个数组， 
function createUserList() {
  return [
    {

      userId: 1,
      avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
      username: 'admin',
      password: '123456',
      desc: '平台管理员',
      roles: ['平台管理员'],
      buttons: ['curser.detail'],
      routes: ['home'],
      token: 'Admin Token'
    },
    {
      userId: 2,
      avatar: "https://example.com/avatar2.png",
      username: 'system',
      password: '123456',
      desc: '系统管理员',
      roles: ['系统管理员'],
      buttons: ['curser.detail', 'curser.user'],
      routes: ['home'],
      token: 'System Token'
    }
  ]

}
//  对外暴露一个数组：数组里面包含两个接口
// 登录的假的接口
//  获取用户信息的假的接口
export default [

  {
    url: "/api/user/logout",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "success"
      }
    }
  },
  {
    url: "/api/user/login",
    method: "post",
    response: ({ body }: { body: { username: string, password: string } }) => {
      // 获取请求体携带过来的用户和密码 
      const { username, password } = body;
      console.log('body信息:', username, password);
      // 调用获取用户信息函数，用于判断是否有此用户
      const checkUser = createUserList().find(
        (item) => {
          return item.username === username && item.password === password;
        }
      );
      if (!checkUser) {
        return {
          code: 500,
          message: "用户名或密码错误"
        }
      }
      const { token } = checkUser
      return {
        code: 200,
        message: "success",
        data: token
      }
    }
  },
  // 获取用户信息的假的接口 
  {
    url: "/api/user/info",
    method: "get",
    response: (request: { headers: { token: string } }) => {
      const token = request.headers.token;
      // 根据token获取用户信息
      const checkUser = createUserList().find(
        (item) => item.token === token
      );
      if (!checkUser) {
        return {
          code: 500,
          message: "用户信息获取失败"
        }
      }
      return {
        code: 200,
        message: "success",
        data: checkUser
      }
    }
  },

] 